//
//  SQTabBarController.m
//  ocCrazy
//
//  Created by 苏青 on 2017/3/23.
//  Copyright © 2017年 dukai. All rights reserved.
//

#import "SQTabBarController.h"
/**首页*/
#import "SQHomeViewController.h"
/*独立隐患*/
#import "SQHiddenDangerViewController.h"
/* 扫码巡检*/
#import "SQScanCodeViewController.h"
/* 功能模块*/
#import "SQFunctionViewController.h"
/**我的*/
#import "SQMyViewController.h"

/**导航控制器*/
#import "BaseNavigationController.h"
//#import "SQOrganizationViewController.h"
//#import "SQIntegralViewController.h"



@interface SQTabBarController ()<UITabBarControllerDelegate>
@end

@implementation SQTabBarController

- (void)viewDidLoad {
  
    [super viewDidLoad];
    
    
    [self createViewControllers];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createViewControllers) name:@"productChange" object:nil];
}

#pragma mark -- 添加视图模块

-(void)createViewControllers{
    NSArray * identityControllerArray;

        
        identityControllerArray = @[@"SQHomeViewController",@"SQHiddenDangerViewController",@"SQScanCodeViewController",@"SQFunctionViewController",@"SQMyViewController"];

    
         
              //    //未选中的图片
    NSArray * identityUnselectedImageNameArray = @[@"home_not",@"hiddenDanger_not",@"code_not",@"function_not",@"my_not"];

              //选中的图片
    NSArray * identitySelectedImageNameArray = @[@"home_sel",@"hiddenDanger_sel",@"code_sel",@"function_sel",@"my_sel"];

              //标题
    NSArray * identityTitleArray = @[@"首页",@"独立隐患",@"",@"功能模块",@"我的"];
              //导航控制器
    NSMutableArray * navArray = [[NSMutableArray alloc]init];
              
      [identityControllerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          
          Class cl = NSClassFromString(obj);
          
          UIViewController * viewController = [[cl alloc]init];
          
          viewController.title = identityTitleArray[idx];
          
          
          UIImage * unselectedImage = [[UIImage imageNamed:identityUnselectedImageNameArray[idx] ]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
          
          viewController.tabBarItem.image = unselectedImage;
          
          
          
          UIImage * selectedImage = [[UIImage imageNamed:identitySelectedImageNameArray[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
          
          viewController.tabBarItem.selectedImage = selectedImage;
          
          UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:viewController];
          
          [navArray addObject:nav];
          
      }];
              
    self.viewControllers = navArray;
#if defined(__IPHONE_15_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_15_0
//    if(@available(iOS 13.0,*)){
//        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
//    }
    if(@available(iOS 15.0, *)) {
        UITabBarAppearance * appearance = [UITabBarAppearance new];
        appearance.backgroundColor = [UIColor colorNamed:@"tabBarBgColor"];
//                  UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];

//                  self.tabBar.scrollEdgeAppearance= appearance;
    }
    
#endif
    
              
    self.tabBar.barStyle = UIBarStyleBlack;
              
              
              
    [UITabBar appearance].translucent = NO;
              
    [[UITabBar appearance] setBarTintColor:[UIColor colorNamed:@"tabBarBgColor"]];
              
    self.tabBar.layer.shadowColor = [UIColor lightGrayColor].CGColor;
//    self.tabBar.layer.shadowOffset = CGSizeMake(0, -1);
    self.tabBar.layer.shadowOpacity =0.3;
              
    self.delegate = self;
              
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorNamed:@"notColor"]} forState:UIControlStateNormal];
              

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorNamed:@"selColor"]} forState:UIControlStateSelected];
   
    if(@available(iOS 13.0,*)){
        UITabBarAppearance * appearance = [[UITabBarAppearance alloc]init];
        // 背景色
        appearance.backgroundColor=[UIColor colorNamed:@"tabBarBgColor"];
//        appearance.selectionIndicatorTintColor = [UIColor colorNamed:@"selColor"];
//        [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorNamed:@"selColor"]} forState:UIControlStateSelected]];
        
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor colorNamed:@"notColor"]};
        // 设置被选中时的颜色
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor colorNamed:@"selColor"]};
        
        self.tabBar.standardAppearance=appearance;
    if(@available(iOS 15.0,*)){
        self.tabBar.scrollEdgeAppearance=appearance;
        
    }
        
    }

}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    if ([tabBarController.tabBarItem.title isEqualToString:@""]) {
        
    }else{
    
    }
    
    return YES;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
